﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
using AForge;
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
using ToolUtil;

namespace zymain
{
    public partial class FrmVideoToImg : Form
    {
        // 设置摄像头参数
        private FilterInfoCollection videoDevices;      // 采集所有摄像头
        public bool isVideo = false;
        public FrmVideoToImg()
        {
            InitializeComponent();
        }

        private void FrmVideoToImg_Load(object sender, EventArgs e)
        {
            if (!isVideo)
            {
                //枚举视频输入设备
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (videoDevices.Count == 0)
                {
                    CameraKay.Text = "没有检测到视频设备！";
                    videoDevices = null;
                }
                foreach (FilterInfo device in videoDevices)
                {
                    CameraKay.Items.Add(device.Name);
                    //CameraKay.Text = device.Name;
                    isVideo = true;
                }
                CameraKay.SelectedIndex = 0;
            }
            string path ="D:\\bookimg\\";
            string[] extensions = new string[] { ".jpg",".png" };
            List<string> list = FileTool.TraverseDirectory(path, extensions);
            foreach(string str in list)
            {
                if (File.Exists(str))
                {
                    // 获取路径
                    string directoryPath = Path.GetDirectoryName(str);
                    Console.WriteLine("路径：" + directoryPath);

                    // 获取文件名
                    string fileName = Path.GetFileName(str);
                    Console.WriteLine("文件名：" + fileName);
                }
            }
        }
        private void ShutCamera()
        {
            if (videoSourcePlayer1.VideoSource != null)
            {
                videoSourcePlayer1.SignalToStop();
                videoSourcePlayer1.WaitForStop();
                videoSourcePlayer1.VideoSource = null;
            }
        }

        private void vpzBtn_Click(object sender, EventArgs e)
        {
            // “拍照”按键代码
            Bitmap img = videoSourcePlayer1.GetCurrentVideoFrame();//拍摄
            pictureBox1.Image = img;
        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            ShutCamera();
            base.Close();
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (_selecting)
            {
                Pen pen = Pens.GreenYellow;
                e.Graphics.DrawRectangle(pen, _selection);
            }
        }
        private bool _selecting;
        private Rectangle _selection;
        //按下鼠标
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _selecting = true;
                _selection = new Rectangle(new System.Drawing.Point(e.X, e.Y), new Size());
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (_selecting)
            {
                _selection.Width = e.X - _selection.X;
                _selection.Height = e.Y - _selection.Y;
                // Redraw the picturebox:
                pictureBox1.Invalidate();
            }
        }
       private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Bitmap bit = new Bitmap(pictureBox1.Width, pictureBox1.Height);//实例化一个和窗体一样大的bitmap
            Graphics g1 = Graphics.FromImage(bit);
            g1.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
            //g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(panel2.Width, panel2.Height));//保存整个窗体为图片
            g1.CopyFromScreen(pictureBox1.PointToScreen(System.Drawing.Point.Empty), System.Drawing.Point.Empty, pictureBox1.Size);
            if (e.Button == MouseButtons.Left &&_selecting &&_selection.Size != new Size())
            {
                pictureBox2.Image = ImgTool.Cut(bit, _selection.X, _selection.Y, _selection.Width, _selection.Height);
                _selecting = false;
            }
            else
                _selecting = false;
        }

        private void camera_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isVideo)
            {
                this.ShutCamera();
                VideoCaptureDevice videoCapture = new VideoCaptureDevice(videoDevices[CameraKay.SelectedIndex].MonikerString);
                videoSourcePlayer1.VideoSource = videoCapture;
                videoSourcePlayer1.Start();
                vpzBtn.Enabled = true;
                closeBtn.Enabled = true;
            }            
        }

        private void video_FormCLosing(object sender, FormClosingEventArgs e)
        {
            // 设置拍照关闭代码
            ShutCamera();
            e.Cancel = false;
        }
    }
}
